package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.AttendLog;
import com.test.dao.AttendLogDao;
import com.test.service.AttendLogService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

/**
 * 考勤记录(AttendLog)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("attendLogService")
public class AttendLogServiceImpl implements AttendLogService {
    @Resource
    private AttendLogDao attendLogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AttendLog queryById(Integer id) {
        return this.attendLogDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param attendLog 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(AttendLog attendLog, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.attendLogDao.count(attendLog)));
        int pageSize = pageRequest.getPageSize();
        int page;
        page = total%pageSize==0? total/pageSize:total/pageSize+1;
        List<AttendLog> attendLogList = attendLogDao.queryAllByLimit(attendLog, pageRequest);
        List<MyObject> objectList = attendLogList.stream().map(attendLog1 -> new MyObject(attendLog1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,objectList,total,page);

    }

    /**
     * 新增数据
     *
     * @param attendLog 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(AttendLog attendLog) {
        return this.attendLogDao.insert(attendLog);
    }

    /**
     * 修改数据
     *
     * @param attendLog 实例对象
     * @return 实例对象
     */
    @Override
    public AttendLog update(AttendLog attendLog) {
        this.attendLogDao.update(attendLog);
        return this.queryById(attendLog.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.attendLogDao.deleteById(id) > 0;
    }

    @Override
    public String selectByName(Integer id) {
        return attendLogDao.selectByName(id);
    }
}
